<?php
/*
 * 商品
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Product;
use think\facade\Config;

class ProductController extends BaseController{

    //商品列表
    public function productList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $p_name = Request::param('p_name');
        $class_one = Request::param('class_one');
        $class_two = Request::param('class_two');
        $class_thr = Request::param('class_thr');
        $supplier = Request::param('supplier');
        $con_person = Request::param('con_person');
        $con_information = Request::param('con_information');
        $res_person = Request::param('res_person');
        $cost_price_min = Request::param('cost_price_min');
        $supply_price_min = Request::param('supply_price_min');
        $cost_price_max = Request::param('cost_price_max');
        $supply_price_max = Request::param('supply_price_max');
        $brand_state = Request::param('brand_state');
        $fs_state = Request::param('fs_state');
        $pre_state = Request::param('pre_state');
        $br_name = Request::param('br_name');
        $state = Request::param('state');
        $p_num = Request::param('p_num');
        $fs_up_status  = Request::param('fs_up_status');
        $brand_up_status  = Request::param('brand_up_status');
        $pre_up_status  = Request::param('pre_up_status');
        
        $where = [];
        $where[] = ['enable','=','1'];
        $where[] = ['type','=',1];
        if(!empty($p_name)){
//            $where[] = ['p_name','=',$p_name];
            $where[] = ['p_name','like',"%$p_name%"];
        }
        if(!empty($state)){
            $where[] = ['state','=',$state];
        }
        if($brand_state==-1){
            $brand_state=0;
            $where[] = ['brand_state','=',$brand_state];
        }elseif($brand_state==1){
            $where[] = ['brand_state','=',$brand_state];
        }
        
        if($brand_up_status==-1){
            $brand_up_state=0;
            $where[] = ['brand_up_status','=',$brand_up_status];
        }elseif($brand_up_status==1){
            $where[] = ['brand_up_status','=',$brand_up_status];
        }
        
        if($fs_up_status==-1){
            $fs_up_status=0;
            $where[] = ['fs_up_status','=',$fs_up_status];
        }elseif($fs_up_status==1){
            $where[] = ['fs_up_status','=',$fs_up_status];
        }
        
        if($pre_up_status==-1){
            $pre_up_state=0;
            $where[] = ['pre_up_status','=',$pre_up_status];
        }elseif($pre_up_status==1){
            $where[] = ['pre_up_status','=',$pre_up_status];
        }
        
        if($fs_state==-1){
            $fs_state=0;
            $where[] = ['fs_state','=',$fs_state];
        }elseif($fs_state==1){
            $where[] = ['fs_state','=',$fs_state];
        }
        
        if($pre_state==-1){
            $pre_state=0;
            $where[] = ['pre_state','=',$pre_state];
        }elseif($pre_state==1){
            $where[] = ['pre_state','=',$pre_state];
        }
        
        if(!empty($br_name)){
            $where[] = ['br_name','=',$br_name];
        }
        if(!empty($p_num)){
            $where[] = ['p_num','=',$p_num];
        }
        if(!empty($class_one)){
            $where[] = ['class_one','=',$class_one];
        }
        if(!empty($class_two)){
            $where[] = ['class_two','=',$class_two];
        }
        if(!empty($class_thr)){
            $where[] = ['class_thr','=',$class_thr];
        }
        if(!empty($supplier)){
            $where[] = ['supplier','=',$supplier];
        }
        if(!empty($con_person)){
            $where[] = ['con_person','=',$con_person];
        }
        if(!empty($con_information)){
            $where[] = ['con_information','=',$con_information];
        }
        if(!empty($res_person)){
            $where[] = ['res_person','=',$res_person];
        }
        if(!empty($cost_price_min) && !empty($cost_price_max)){
            $where[] = ['cost_price','>=',$cost_price_min];
            $where[] = ['cost_price','<=',$cost_price_max];
        }
        if(!empty($supply_price_min) && !empty($supply_price_max)){
            $where[] = ['supply_price','>=',$supply_price_min];
            $where[] = ['supply_price','<=',$supply_price_max];
        }

        $list = Product::productList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                if(!empty($value["pre_sale_time"])){
                    $list[$key]["pre_sale_time"] = date( "Y-m-d H:i:s",$value["pre_sale_time"]);
                }
                if(!empty($value[$key]['img'])){
                    $list[$key]['img'] =  $value['img'];
                }
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Product::productCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //商品添加
    public function productAdd(){

        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $p_name = Request::param('p_name');
        $p_num = Request::param('p_num');
        $class_one = Request::param('class_one');
        $class_two = Request::param('class_two');
        $class_thr = Request::param('class_thr');
        $supplier = Request::param('supplier');
        $supplier_id = Request::param('supplier_id');
        $con_person = Request::param('con_person');
        $con_information = Request::param('con_information');
        $res_person = Request::param('res_person');
        $cost_price = Request::param('cost_price');
        $link = Request::param('link');
        $weight = Request::param('weight');
        $volume = Request::param('volume');
        $remarks = Request::param('remarks');
        $img = Request::param('img');
        $supply_price = Request::param('supply_price');
        $zone_type = Request::param('zone_type');
        $shi_type = Request::param('shi_type');
        $brand_state = Request::param('brand_state');
        $fs_state = Request::param('fs_state');
        
        $br_name = Request::param('br_name');
        $br_id = Request::param('br_id');
        $inventory = Request::param('inventory');
        $vir_sales = Request::param('vir_sales');
        $commission_price = Request::param('commission_price');
        $details_img_json = Request::param('details_img_json');
        $specifications = Request::param('specifications');
        $pre_sale_time = Request::param('pre_sale_time');
        $pre_state = Request::param('pre_state');
        if(empty($brand_state)){
            $brand_state = 0;
        }
        if($brand_state == -1){
            $brand_state = 0;
        }
        if(empty($fs_state)){
            $fs_state = 0;
        }
        if($fs_state==-1){
            $fs_state = 0;
        }
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        
        if(empty($type)){
            $type = 1;
        }
        
        if(!empty($pre_sale_time) && $pre_state==1){
            $pre_sale_time = strtotime($pre_sale_time);
        }
        
        if($pre_state==1){
            if($commission_price<10){
                return json(['code' => 0, 'msg' => lang('秒杀专商品佣金必须大于等于10元'), 'data' => '', 'count' => '']);
            }
        }

        
        $time = time();
        $p_num = 'P'.date('YmdHis',time()). rand(10000,99999);
        $data = [
            'p_num' => $p_num,
            'p_name' => $p_name,
            'enable' => $enable,
            'addtime' => $time,
            'br_id' => $br_id,
            'br_name' => $br_name,
            'type' => $type,
            'zone_type' => $zone_type,
            'shi_type' => $shi_type,
            'class_one' => $class_one,
            'class_two' => $class_two,
            'class_thr' => $class_thr,
            'supplier_id' => $supplier_id,
            'supplier' => $supplier,
            'con_person' => $con_person,
            'con_information' => $con_information,
            'res_person' => $res_person,
            'cost_price' => $cost_price,
            'link' => $link,
            'supply_price' => $supply_price,
            'weight' => $weight,
            'volume' => $volume,
            'remarks' => $remarks,
            'brand_state' => $brand_state,
            'fs_state' => $fs_state,
            'pre_state' => $pre_state,
            'inventory' => $inventory,
            'vir_sales' => $vir_sales,
            'commission_price' => $commission_price,
            'img' => $img,
            'details_img_json' => $details_img_json,
            'specifications' => $specifications,
            'pre_sale_time' => $pre_sale_time,
        ];

        Product::productAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //商品SKU列表
    public function proSKUList(){
        $a_uid = Request::param('a_uid');
        $pid = Request::param('pid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $where = [];
        $where[] = ['enable','=','1'];
        $where[] = ['type','=','5'];
        $where[] = ['tie_up_pid','=',$pid];
        
        $list = Product::productList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                if(!empty($value["pre_sale_time"])){
                    $list[$key]["pre_sale_time"] = date( "Y-m-d H:i:s",$value["pre_sale_time"]);
                }
                if(!empty($value[$key]['img'])){
                    $list[$key]['img'] =  $value['img'];
                }
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Product::productCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //商品SKU添加
    public function proSKUAdd(){
        
        $a_uid = Request::param('a_uid');
        $pid = Request::param('pid');
        $enable = Request::param('enable');
        $p_name = Request::param('p_name');
        $p_num = Request::param('p_num');
        $class_one = Request::param('class_one');
        $class_two = Request::param('class_two');
        $class_thr = Request::param('class_thr');
        $supplier_id = Request::param('supplier_id');
        $supplier = Request::param('supplier');
        $con_person = Request::param('con_person');
        $con_information = Request::param('con_information');
        $res_person = Request::param('res_person');
        $link = Request::param('link');
        $weight = Request::param('weight');
        $volume = Request::param('volume');
        $remarks = Request::param('remarks');
        $img = Request::param('img');
        $zone_type = Request::param('zone_type');
        $shi_type = Request::param('shi_type');
        $brand_state = Request::param('brand_state');
        $fs_state = Request::param('fs_state');
//        $type = Request::param('type');
        $br_name = Request::param('br_name');
        $br_id = Request::param('br_id');
        $inventory = Request::param('inventory');
        $vir_sales = Request::param('vir_sales');
        $details_img_json = Request::param('details_img_json');
        $pre_sale_time = Request::param('pre_sale_time');
        $pre_state = Request::param('pre_state');
        
        $supply_price = Request::param('supply_price');
        $cost_price = Request::param('cost_price');
        $commission_price = Request::param('commission_price');
        $specifications = Request::param('specifications');
        if(empty($brand_state)){
            $brand_state = 0;
        }
        if($brand_state == -1){
            $brand_state = 0;
        }
        if(empty($fs_state)){
            $fs_state = 0;
        }
        if($fs_state==-1){
            $fs_state = 0;
        }
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        
        if(empty($type)){
            $type = 1;
        }
        
        if(!empty($pre_sale_time) && $pre_state==1){
            $pre_sale_time = strtotime($pre_sale_time);
        }
        else{
            $pre_sale_time = 0;
        }
        
        if($pre_state==1){
            if($commission_price<10){
                return json(['code' => 0, 'msg' => lang('秒杀专商品佣金必须大于等于10元'), 'data' => '', 'count' => '']);
            }
        }

        
        $time = time();
        $p_num = 'P'.date('YmdHis',time()). rand(10000,99999);
        $data = [
            'p_num' => $p_num,
            'p_name' => $p_name,
            'enable' => $enable,
            'addtime' => $time,
            'br_id' => $br_id,
            'br_name' => $br_name,
            'type' => 5,
            'zone_type' => $zone_type,
            'shi_type' => $shi_type,
            'class_one' => $class_one,
            'class_two' => $class_two,
            'class_thr' => $class_thr,
            'supplier_id' => $supplier_id,
            'supplier' => $supplier,
            'con_person' => $con_person,
            'con_information' => $con_information,
            'res_person' => $res_person,
            'cost_price' => $cost_price,
            'link' => $link,
            'supply_price' => $supply_price,
            'weight' => $weight,
            'volume' => $volume,
            'remarks' => $remarks,
            'brand_state' => $brand_state,
            'fs_state' => $fs_state,
            'pre_state' => $pre_state,
            'inventory' => $inventory,
            'vir_sales' => $vir_sales,
            'commission_price' => $commission_price,
            'img' => $img,
            'details_img_json' => $details_img_json,
            'specifications' => $specifications,
            'pre_sale_time' => $pre_sale_time,
            'tie_up_pid' => $pid,
        ];

        Product::productAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //商品详情
    public function productInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Product::productInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);
            if(!empty($info['img'])){
                $info['img'] =  $info['img'];
            }
            
            if(!empty($info["pre_sale_time"])){
                $info["pre_sale_time"] = date( "Y-m-d H:i:s",$info["pre_sale_time"]);
            }
            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    
    //库存
    public function productInventor(){
        $a_uid = Request::param('a_uid');//管理员ID
        $id = Request::param('id');//活动ID
        $inventory = Request::param('inventory');
        $type = Request::param('type');
        
        
        if($type==1){
            Product::productInc(['id'=>$id],'inventory',intval($inventory));
        }
        else{
            Product::productDec(['id'=>$id],'inventory',intval($inventory));
        }
        

        return json(['code' => 1, 'msg' => lang(''), 'data' => [], 'count' =>'']);
    }
    
    //商品顶置 --顶置操作/编辑操作
    public function productUpEdit(){
        $a_uid = Request::param('a_uid');//管理员ID
        $p_num = Request::param('p_num');//活动编号
        $upType = Request::param('upType');//顶置类型
        $rank = Request::param('rank');//顶置排序
        $id = Request::param('id');//活动编号
        


        if(intval($rank) < 0){
            return json(['code' => 0, 'msg' => lang('排序参数错误'), 'data' => [], 'count' =>'']);
        }

        $upActivityData = [];

        //活动 及 活动状态检索
        //$activity = Product::productInfo(['p_num'=>$p_num]);
        $activity = Product::productInfo(['id'=>$id]);
        if(empty($activity)){
            return json(['code' => 0, 'msg' => lang('商品不存在'), 'data' => [], 'count' =>'']);
        }
        if($activity['state'] < 5){
            return json(['code' => 0, 'msg' => lang('商品尚未上线'), 'data' => [], 'count' =>'']);
        }
        if($activity['state'] >= 10){
            return json(['code' => 0, 'msg' => lang('商品已下线'), 'data' => [], 'count' =>'']);
        }

        //顶置名额检索
        $where = [];
        if($upType == 'brand_up_status'){//品牌
            if($activity['brand_state'] != 1){
                return json(['code' => 0, 'msg' => lang('商品不能在品牌专区顶置'), 'data' => [], 'count' =>'']);
            }
            /*if($activity['com_info']['d_price'] != 0){
                return json(['code' => 0, 'msg' => lang('非0元活动禁止加入白菜专区'), 'data' => [], 'count' =>'']);
            }*/
            $where[] = ['brand_up_status','=',1];
            //$where[] = ['type','in','1,2,3'];
            $upActivityData['brand_up_status'] = 1;
            $upActivityData['brand_up_rank'] = $rank;
            $upActivityData['brand_up_time'] = time();
        }elseif ($upType == 'fs_up_status'){//秒杀
            if($activity['fs_state'] != 1){
                return json(['code' => 0, 'msg' => lang('商品不能在秒杀专区顶置'), 'data' => [], 'count' =>'']);
            }
            $where[] = ['fs_up_status','=',1];
            //$where[] = ['type','in','4'];
            $upActivityData['fs_up_status'] = 1;
            $upActivityData['fs_up_rank'] = $rank;
            $upActivityData['fs_up_time'] = time();
        }elseif ($upType == 'pre_up_status'){//预售商品
            if($activity['pre_state'] != 1){
                return json(['code' => 0, 'msg' => lang('商品不能在预售专区顶置'), 'data' => [], 'count' =>'']);
            }
            $where[] = ['pre_up_status','=',1];
            //$where[] = ['type','in','4'];
            $upActivityData['pre_up_status'] = 1;
            $upActivityData['pre_up_rank'] = $rank;
            $upActivityData['pre_up_time'] = time();
        }
        $where[] = ['state','=','5'];
        $upActivityCount = Product::productCount($where);
        if($upActivityCount >= 10){
            return json(['code' => 0, 'msg' => lang('顶置名额已满'), 'data' => [], 'count' =>'']);
        }

        //商品顶置修改
        Product::productEdit(['id'=>$activity['id']],$upActivityData);

        return json(['code' => 1, 'msg' => lang(''), 'data' => [], 'count' =>'']);
    }

    //取消商品顶置
    public function productUpOff(){
        $a_uid = Request::param('a_uid');//管理员ID
        $pid = Request::param('pid');//活动id
        $upType = Request::param('upType');//顶置类型
        //活动 及 活动状态检索
        $activity = Product::productInfo(['id'=>$pid]);
        if(empty($activity)){
            return json(['code' => 0, 'msg' => lang('活动不存在'), 'data' => [], 'count' =>'']);
        }

        $upActivityData = [];

        if($upType == 'brand_up_status'){//品牌
            if($activity['brand_up_status'] != 1){
                return json(['code' => 0, 'msg' => lang('此活动暂无品牌专区顶置设置'), 'data' => [], 'count' =>'']);
            }
            $upActivityData['brand_up_status'] = 0;
            $upActivityData['brand_up_rank'] = 0;
        }elseif ($upType == 'fs_up_status'){//0元试用
            if($activity['fs_up_status'] != 1){
                return json(['code' => 0, 'msg' => lang('此活动暂无秒杀专区顶置设置'), 'data' => [], 'count' =>'']);
            }
            $upActivityData['fs_up_status'] = 0;
            $upActivityData['fs_up_rank'] = 0;
        }elseif ($upType == 'pre_up_status'){//0元试用
            if($activity['pre_up_status'] != 1){
                return json(['code' => 0, 'msg' => lang('此活动暂无预售专区顶置设置'), 'data' => [], 'count' =>'']);
            }
            $upActivityData['pre_up_status'] = 0;
            $upActivityData['pre_up_rank'] = 0;
        }/*else{//全部商品
            if($activity['all_up_status'] != 1){
                return json(['code' => 0, 'msg' => lang('此活动暂无全部商品专区顶置设置'), 'data' => [], 'count' =>'']);
            }
            $upActivityData['all_up_status'] = 0;
            $upActivityData['all_up_rank'] = 0;
        }*/
        //取消商品顶置修改
        Product::productEdit(['id'=>$activity['id']],$upActivityData);

        return json(['code' => 1, 'msg' => lang(''), 'data' => [], 'count' =>'']);
    }
    
    
    //商品修改
    public function productEdit(){
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $p_name = Request::param('p_name');
        $class_one = Request::param('class_one');
        $class_two = Request::param('class_two');
        $class_thr = Request::param('class_thr');
        $supplier_id = Request::param('supplier_id');
        $supplier = Request::param('supplier');
        $con_person = Request::param('con_person');
        $con_information = Request::param('con_information');
        $res_person = Request::param('res_person');
        $cost_price = Request::param('cost_price');
        $link = Request::param('link');
        $weight = Request::param('weight');
        $volume = Request::param('volume');
        $img = Request::param('img');
        $remarks = Request::param('remarks');
        $id = Request::param('id');
        $supply_price = Request::param('supply_price');
        $br_name = Request::param('br_name');
        $br_id = Request::param('br_id');
        $brand_state = Request::param('brand_state');
        $fs_state = Request::param('fs_state');
        $vir_sales = Request::param('vir_sales');
        $state = Request::param('state');
        $commission_price = Request::param('commission_price');
        $details_img_json = Request::param('details_img_json');
        $specifications = Request::param('specifications');
        $pre_sale_time = Request::param('pre_sale_time');
        $pre_state = Request::param('pre_state');
        $type = Request::param('type');
        $update = [];
        $update_sku = [];
        if(!empty($p_name)){
            $update['p_name'] = $p_name;
            $update_sku['p_name'] = $p_name;
        }
//        if(!empty($type)){
//            $update['type'] = $type;
//        }

        if(!empty($br_name)){
            $update['br_name'] = $br_name;
            $update_sku['br_name'] = $br_name;
        }
        if(!empty($br_id)){
            $update['br_id'] = $br_id;
            $update_sku['br_id'] = $br_id;
        }
        if(!empty($vir_sales)){
            $update['vir_sales'] = $vir_sales;
//            $update_sku['vir_sales'] = $vir_sales;
        }
        if(!empty($details_img_json)){
            $update['details_img_json'] = $details_img_json;
            $update_sku['details_img_json'] = $details_img_json;
        }
        if(!empty($specifications)){
            $update['specifications'] = $specifications;
//            $update_sku['specifications'] = $specifications;
        }
        if(!empty($brand_state)){
            if($brand_state == -1){
                $brand_state = 0;
            }
            
            $update['brand_state'] = $brand_state;
            $update_sku['brand_state'] = $brand_state;
        }

        if(!empty($fs_state)){
            if($fs_state == -1){
                $fs_state = 0;
            }
            
            $update['fs_state'] = $fs_state;
            $update_sku['fs_state'] = $fs_state;
        }
        if(!empty($state)){
            if($state == -1){
                $state = 0;
            }
            
            $update['state'] = $state;
            $update_sku['state'] = $state;
        }
        
        if(!empty($commission_price)){
            if($commission_price == -1){
                $commission_price = 0;
            }
            $update['commission_price'] = $commission_price;
//            $update_sku['commission_price'] = $commission_price;
        }
        
        if(!empty($class_one)){
            $update['class_one'] = $class_one;
            $update_sku['class_one'] = $class_one;
        }
        
        if(!empty($class_two)){
            $update['class_two'] = $class_two;
            $update_sku['class_two'] = $class_two;
        }
        
        if(!empty($class_thr)){
            $update['class_thr'] = $class_thr;
            $update_sku['class_thr'] = $class_thr;
            
        }
        
        /*if(!empty($supplier)){
            $update['address'] = $address;
        }*/
        if(!empty($supply_price)){
            $update['supply_price'] = $supply_price;
//            $update_sku['supply_price'] = $supply_price;
        }
        if(!empty($supplier_id)){
            $update['supplier_id'] = $supplier_id;
            $update_sku['supplier_id'] = $supplier_id;
        }
        if(!empty($supplier)){
            $update['supplier'] = $supplier;
            $update_sku['supplier'] = $supplier;
        }
        if(!empty($con_person)){
            $update['con_person'] = $con_person;
            $update_sku['con_person'] = $con_person;
        }
        if(!empty($con_information)){
            $update['con_information'] = $con_information;
            $update_sku['con_information'] = $con_information;
        }
        if(!empty($res_person)){
            $update['res_person'] = $res_person;
            $update_sku['res_person'] = $res_person;
        }
        if(!empty($cost_price)){
            $update['cost_price'] = $cost_price;
//            $update_sku['cost_price'] = $cost_price;
        }
        if(!empty($link)){
            $update['link'] = $link;
            $update_sku['link'] = $link;
        }
        if(!empty($weight)){
            $update['weight'] = $weight;
            $update_sku['weight'] = $weight;
        }
        if(!empty($volume)){
            $update['volume'] = $volume;
            $update_sku['volume'] = $volume;
        }
        if(!empty($res_person)){
            $update['res_person'] = $res_person;
            $update_sku['res_person'] = $res_person;
        }
        if(!empty($remarks)){
            $update['remarks'] = $remarks;
            $update_sku['remarks'] = $remarks;
        }
        if(!empty($img)){
            $update['img'] = $img;
            $update_sku['img'] = $img;
        }
        
        if(!empty($pre_state)){
            $update['pre_state'] = $pre_state;
            $update_sku['pre_state'] = $pre_state;
        }
        if(!empty($pre_sale_time) && $pre_state==1){
            $update['pre_sale_time'] = strtotime($pre_sale_time);
            $update_sku['pre_sale_time'] = strtotime($pre_sale_time);
        }
        
        if($enable==-1){
            $update['enable'] = 0;
            $update_sku['enable'] = 0;
        }else{
            $update['enable'] = 1;
            $update_sku['enable'] = 1;
        }

        if(empty($type)){
            $type = 1;
        }
        if($type==1){
            Product::productEdit($id,$update);
            Product::productSKUEdit($id,$update_sku);
        }
        
        if($type==5){
            Product::productEdit($id,$update);
        }
        
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //物料详情
    public function materialInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Product::productInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);
            if(is_array($info['img_json'])){
                foreach ($info['img_json'] as $k => $v) {
                    if(strpos($info['img_json'][$k],"img.alicdn.com") !== false){
        //                $listinfo[$key]['commodity']['picture_url'][$k] =  Config::get('app.pomegranate_url').$value['commodity']['picture'][$k];
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                    elseif(strpos($info['img_json'][$k],"http") !== false){
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                    else{
                        $info['img_json'][$k] =  $info['img_json'][$k];
                    }
                }
            }

        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //物料修改
    public function materialEdit(){
        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        
        $img_json = Request::param('img_json');
        $det_info = Request::param('det_info');
        $live_script = Request::param('live_script');
        if(!empty($img_json)){
            $update['img_json'] = $img_json;
        }
        if(!empty($det_info)){
            $update['det_info'] = $det_info;
        }
        if(!empty($live_script)){
            $update['live_script'] = $live_script;
        }
        
        Product::productEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
